﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Validation
{
	using System.Collections.Generic;
	using System.Linq;

	public class ValidationResult
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ValidationResult"/> class.
		/// </summary>
		public ValidationResult()
		{
			this.Errors = new Dictionary<string, IList<ValidationError>>();
		}

		/// <summary>
		/// Gets or sets the errors.
		/// </summary>
		/// <value>The errors.</value>
		public Dictionary<string, IList<ValidationError>> Errors
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets a value indicating whether this instance has errors.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance has errors; otherwise, <c>false</c>.
		/// </value>
		public bool HasErrors
		{
			get
			{
				return this.Errors.Count > 0;
			}
		}

		/// <summary>
		/// Determines whether the specified bind has errors.
		/// </summary>
		/// <param name="bind">The bind.</param>
		/// <returns>
		/// 	<c>true</c> if the specified bind has errors; otherwise, <c>false</c>.
		/// </returns>
		public bool HasErrorsFor(string bind)
		{
			return (this.Errors.ContainsKey(bind));
		}

		/// <summary>
		/// Gets the errors for the specified bind.
		/// </summary>
		/// <param name="bind">The bind.</param>
		/// <returns></returns>
		public ValidationError[] GetErrorsFor(string bind)
		{
			return HasErrorsFor(bind) ? this.Errors[bind].ToArray() : null;
		}

		/// <summary>
		/// Adds the specified error.
		/// </summary>
		/// <param name="error">The error.</param>
		public void Add(ValidationError error)
		{
			if (!this.Errors.ContainsKey(error.Bind))
			{
				this.Errors.Add(error.Bind, new List<ValidationError>());
			}

			this.Errors[error.Bind].Add(error);
		}
	}
}